package com.qf.mywatch.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qf.mywatch.adapter.FragPagerAdaper;
import com.qf.mywatch.R;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 * 表库
 */
public class WatchLibraryFragment extends Fragment implements TabLayout.OnTabSelectedListener  {

    private Toolbar toolbar;
    private TabLayout tl;
    private ViewPager viewPager;
    private ArrayList<Fragment> list_frag;
    private FragPagerAdaper adaper;
    private String[] arr;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_watchlibrary,null);
        initView(view);
        toolbarView();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setData();
    }
    private void toolbarView() {
        toolbar.setBackgroundColor(Color.BLACK);
        toolbar.inflateMenu(R.menu.menu_search);
    }
    private void setData() {

        list_frag.add(PinFragment.getFragment(arr[0]));
        list_frag.add(ShaiFragment.getFragment(arr[1]));

        adaper = new FragPagerAdaper(getActivity().getSupportFragmentManager(), list_frag, arr);
        viewPager.setAdapter(adaper);
        tl.setTabTextColors(Color.WHITE, Color.GRAY);//未选中灰色，选中绿色
        tl.setSelectedTabIndicatorColor(Color.WHITE);//设置小滑块颜色
        tl.setSelectedTabIndicatorHeight(5);//设置小滑块高度
        tl.setupWithViewPager(viewPager);
        tl.addOnTabSelectedListener(this);
    }
    //控件实例化
    private void initView(View view) {
        toolbar= (Toolbar) view.findViewById(R.id.watch_frag_tb);
        tl = (TabLayout) view.findViewById(R.id.watch_frag_tl);
        viewPager = (ViewPager) view.findViewById(R.id.watch_frag_vp);
        list_frag = new ArrayList<>();
        arr = getResources().getStringArray(R.array.watchfrag);
    }


    //TabLayout改变
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (tab.getPosition() != arr.length) {
            viewPager.setCurrentItem(tab.getPosition(), true);
        }
    }
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }
    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }


}
